var mpDesc = '手机群录,致力于团体之间成员的通讯信息的在线管理、查找、分享';
var memDesc = '点击页面内电话号码可快速拨号。';
var groupDesc = '';

function getSignature(jsApiList, callback) {
    if (jsApiList == null || jsApiList.length == 0) {
        jsApiList = [
            'checkJsApi', 'scanQRCode', 'closeWindow',
            'onMenuShareTimeline', 'onMenuShareAppMessage',
            'hideOptionMenu', 'showOptionMenu', 'hideMenuItems',
            'showMenuItems', 'hideAllNonBaseMenuItem', 'showAllNonBaseMenuItem'
        ]
    }
    var data = {
        url: location.href
    };
    $.ajax({
        url: $.httpRootPath() + 'share/getJsApiSignature',
        type: 'GET',
        data: data,
        dataType: 'json',
        timeout: 15000,
        success: function (r) {
            r = r.wxConfig;
            wx.config({
                debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来，若要查看传入的参数在pc端打开，参数信息会通过log打出，仅在pc端时才会打印。
                appId: r.appId, // 必填，公众号的唯一标识
                timestamp: r.timestamp, // 必填，生成签名的时间戳
                nonceStr: r.nonceStr, // 必填，生成签名的随机串
                signature: r.signature, //必填，签名，见附录1
                jsApiList: jsApiList
                // 必填，需要使用的JS接口列表，所有JS接口列表见附录2
            });
            wx.ready(function () {
                console.log("errorToast(\"获取分享参数完成\",1);");
                callback()
            })
        },
        error: function (e) {
            console.log(e);
        }
    });
}

function shareMobileGroup(fromUser) {
    var title = '手机群录,轻松找到那个她';
    var desc = mpDesc;
    var imgUrl = $.httpStaticPath() + 'common/img/3.jpg';
    var link = $.httpRootPath() + 'info/wechat?fromUser=' + fromUser;
    fromUser = fromUser == "" ? GetQueryString("fromUser") : fromUser;
    wx.onMenuShareAppMessage({
        title: title, // 分享标题
        desc: desc, // 分享描述
        link: link, // 分享链接，该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
        imgUrl: imgUrl, // 分享图标
        type: 'link', // 分享类型,music、video或link，不填默认为link
        success: function () {
            // 用户确认分享后执行的回调函数
            console.log("用户分享")
            var data = {
                fromPage: location.href,
                fromUser: fromUser,
                shareType: "onMenuShareAppMessage",
                content: title
            };
            addShareRecord(data);
        },
        cancel: function () {
            // 用户取消分享后执行的回调函数
            console.log("用户取消分享")
        }
    });
    wx.onMenuShareTimeline({
        title: title, // 分享标题
        link: link,// 分享链接，该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
        imgUrl: imgUrl, // 分享图标
        success: function () {
            var data = {
                fromPage: location.href,
                fromUser: fromUser,
                shareType: "onMenuShareTimeline",
                content: title
            };
            addShareRecord(data);
        },
    });
}

function shareMemberInfo(memberNum, memName, fromUser, imgUrl) {
    memName = memName == "" ? memberNum : memName;
    var title = "【成员•" + memName + "】";
    var desc = "查看[" + memberNum + "]详细信息" + "，" + memDesc;
    var link = $.httpRootPath() + 'share/member/' + memberNum + '?fromUser=' + fromUser;
    fromUser = fromUser == "" ? GetQueryString("fromUser") : fromUser;
    imgUrl = imgUrl == "" ? $.httpStaticPath() + 'common/icon/info1.png' : imgUrl;
    wx.onMenuShareAppMessage({
        title: title, // 分享标题
        desc: desc, // 分享描述
        link: link, // 分享链接，该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
        imgUrl: imgUrl, // 分享图标
        type: 'link', // 分享类型,music、video或link，不填默认为link
        success: function () {
            // 用户确认分享后执行的回调函数
            var data = {
                fromPage: location.href,
                fromUser: fromUser,
                shareType: "onMenuShareAppMessage",
                content: title,
            };
            addShareRecord(data);
        },
        cancel: function () {
            // 用户取消分享后执行的回调函数
        }
    });
    wx.onMenuShareTimeline({
        title: title, // 分享标题
        link: link, // 分享链接，该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
        imgUrl: imgUrl, // 分享图标
        success: function () {
            var data = {
                fromPage: location.href,
                fromUser: fromUser,
                shareType: "onMenuShareTimeline"
            };
            addShareRecord(data);
        },
    });
}

function shareGroupInfo(orgId, orgName, introduce, fromUser, imgUrl, mark) {
    var title = '【组织•' + orgName + '】';
    introduce = getInfoStr(introduce);
    var desc_default = "  点击查看该组织详情\n" + "公众号内输入标示[" + mark + "]或扫描二维码可加入该组织" + groupDesc;
    var desc = introduce == "" ? desc_default : introduce;


    var link = $.httpRootPath() + 'share/group/' + orgId + "?fromUser=" + fromUser
    fromUser = fromUser == "" ? GetQueryString("fromUser") : fromUser;
    imgUrl = imgUrl == "" ? $.httpStaticPath() + 'common/icon/group.png' : imgUrl;
    wx.onMenuShareAppMessage({
        title: title, // 分享标题
        desc: desc, // 分享描述
        link: link, // 分享链接，该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
        imgUrl: imgUrl, // 分享图标
        type: 'link', // 分享类型,music、video或link，不填默认为link
        success: function () {
            // 用户确认分享后执行的回调函数
            console.log("用户分享")
            var data = {
                fromPage: location.href,
                fromUser: fromUser,
                shareType: "onMenuShareAppMessage",
                content: desc
            };
            addShareRecord(data);
        },
        cancel: function () {
            // 用户取消分享后执行的回调函数
            console.log("用户取消分享")
        }
    });
    wx.onMenuShareTimeline({
        title: title, // 分享标题
        link: link,// 分享链接，该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
        imgUrl: imgUrl, // 分享图标
        success: function () {
            var data = {
                fromPage: location.href,
                fromUser: fromUser,
                shareType: "onMenuShareTimeline",
                content:title
            };
            addShareRecord(data);
        },
    });
}

function shareNoticeInfo(notId, title, content, fromUser, imgUrl) {
    title = title == "" ? '未命名公告' : title;

    var desc = getInfoStr(content);

    var link = $.httpRootPath() + 'share/notice/' + notId + "?fromUser=" + fromUser;
    fromUser = fromUser == "" ? GetQueryString("fromUser") : fromUser;
    imgUrl = imgUrl == "" ? $.httpStaticPath() + 'common/icon/notice4.png' : imgUrl;
    wx.onMenuShareAppMessage({
        title: title, // 分享标题
        desc: desc, // 分享描述
        link: link, // 分享链接，该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
        imgUrl: imgUrl, // 分享图标
        type: 'link', // 分享类型,music、video或link，不填默认为link
        success: function () {
            // 用户确认分享后执行的回调函数
            console.log("用户分享");
            var data = {
                fromPage: location.href,
                fromUser: fromUser,
                shareType: "onMenuShareAppMessage",
                content: title
            };
            addShareRecord(data);
        },
        cancel: function () {
            // 用户取消分享后执行的回调函数
            console.log("用户取消分享")
        }
    });
    wx.onMenuShareTimeline({
        title: title, // 分享标题
        link: link,// 分享链接，该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
        imgUrl: imgUrl, // 分享图标
        success: function () {
            var data = {
                fromPage: location.href,
                fromUser: fromUser,
                shareType: "onMenuShareTimeline"
            };
            addShareRecord(data);
        },
    });
}

function shareTempMsgInfo(temId, title, content, fromUser, imgUrl) {
    title = title == "" ? '未命名推送' : title;

    var desc = getInfoStr(content);

    var link = $.httpRootPath() + 'share/tempMsg/' + temId + "?fromUser=" + fromUser;
    fromUser = fromUser == "" ? GetQueryString("fromUser") : fromUser;
    imgUrl = imgUrl == "" ? $.httpStaticPath() + 'common/icon/notice1.png' : imgUrl;
    wx.onMenuShareAppMessage({
        title: title, // 分享标题
        desc: desc, // 分享描述
        link: link, // 分享链接，该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
        imgUrl: imgUrl, // 分享图标
        type: 'link', // 分享类型,music、video或link，不填默认为link
        success: function () {
            // 用户确认分享后执行的回调函数
            console.log("用户分享")
            var data = {
                fromPage: location.href,
                fromUser: fromUser,
                shareType: "onMenuShareAppMessage",
                content: title
            };
            addShareRecord(data);
        },
        cancel: function () {
            // 用户取消分享后执行的回调函数
            console.log("用户取消分享")
        }
    });
    wx.onMenuShareTimeline({
        title: title, // 分享标题
        link: link,// 分享链接，该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
        imgUrl: imgUrl, // 分享图标
        success: function () {
            var data = {
                fromPage: location.href,
                fromUser: fromUser,
                shareType: "onMenuShareTimeline"
            };
            addShareRecord(data);
        },
    });
}

function addShareRecord(data) {
    console.log(data);
    $(".loading").show();
    $.ajax({
        url: $.httpRootPath() + 'rest/share/add',
        type: 'POST',
        dataType: 'json',
        data: data,
        timeout: 15000,
        cache: false,
        success: function (r) {
            console.log(r);
        },
        error: function (e) {
            console.log("记录失败");
        },
        complete: function () {
            $(".loading").hide();
        }
    });
}

function getInfoStr(content) {
    content = content.replace(/<[^>]+>/g, "").replace(/\s/g, "");
    content = content.length > 50 ? content.substring(0, 50) : content;
    return content;
}

/*
附录2-所有JS接口列表
版本1.0.0接口
*/
function getJsApiList() {
    return
    ['onMenuShareTimeline', 'onMenuShareAppMessage', "onMenuShareQQ", "onMenuShareWeibo", "onMenuShareQZone",
        "startRecord", "stopRecord", "onVoiceRecordEnd", "playVoice", "pauseVoice", "stopVoice", "onVoicePlayEnd",
        "uploadVoice", "downloadVoice", "chooseImage", "previewImage", "uploadImage", "downloadImage", "translateVoice",
        "getNetworkType", "openLocation", "getLocation", "hideOptionMenu", "showOptionMenu", "hideMenuItems", "showMenuItems",
        "hideAllNonBaseMenuItem", "showAllNonBaseMenuItem", "closeWindow", "scanQRCode", "chooseWXPay", "openProductSpecificView",
        "addCard", "chooseCard", "openCard"];
}

/*
附录3-所有菜单项列表

基本类
举报: "menuItem:exposeArticle"
调整字体: "menuItem:setFont"
日间模式: "menuItem:dayMode"
夜间模式: "menuItem:nightMode"
刷新: "menuItem:refresh"
查看公众号（已添加）: "menuItem:profile"
查看公众号（未添加）: "menuItem:addContact"

传播类
发送给朋友: "menuItem:share:appMessage"
分享到朋友圈: "menuItem:share:timeline"
分享到QQ: "menuItem:share:qq"
分享到Weibo: "menuItem:share:weiboApp"
收藏: "menuItem:favorite"
分享到FB: "menuItem:share:facebook"
分享到 QQ 空间/menuItem:share:QZone

保护类
编辑标签: "menuItem:editTag"
删除: "menuItem:delete"
复制链接: "menuItem:copyUrl"
原网页: "menuItem:originPage"
阅读模式: "menuItem:readMode"
在QQ浏览器中打开: "menuItem:openWithQQBrowser"
在Safari中打开: "menuItem:openWithSafari"
邮件: "menuItem:share:email"
一些特殊公众号: "menuItem:share:brand"
*/